//
//  TJStatusViewCell.swift
//  TJSinaWeibo
//
//  Created by 唐后能 on 16/4/14.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit
import SnapKit

class TJStatusViewCell: UITableViewCell {

    //记录底部view的顶部约束.
    var bottomViewTopCons:Constraint?
    
    // 微博数据.
    var status:TJStatus? {
    
        didSet {
            // 将微博数据传递给originView来显示.
            originView.status = status
            
            
            // 更新底部View的参照.
            bottomViewTopCons?.uninstall()
            if let retweetStatus = status?.retweeted_status   { //如果有转发微博.
                
                // 显示retweeteView.
                retweeteView.hidden = false
                // 将被转发微博数据传递给retweeteView.
                retweeteView.retweeteStatus = retweetStatus
                // 更新约束.
                bottomView.snp_makeConstraints(closure: { (make) -> Void in
                   bottomViewTopCons = make.top.equalTo(retweeteView.snp_bottom).constraint
                })
                
            }else { //如果没有转发微博.
                // 隐藏retweeteView
                retweeteView.hidden = true
                // 重新设置约束.
                bottomView.snp_makeConstraints(closure: { (make) -> Void in
                   bottomViewTopCons = make.top.equalTo(originView.snp_bottom).constraint
                })
            }
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
        backgroundColor = UIColor(white: 237/255.0, alpha: 1.0)
    }
    
    // 添加在控件.
    private func setupUI () {
    
        // 1,添加子控件-->注意添加到contentView上面
        contentView.addSubview(originView)
        contentView.addSubview(retweeteView)
        contentView.addSubview(bottomView)
        
        // 2,设置约束.
        // originView.
        originView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(contentView)
            make.top.equalTo(contentView).offset(TJStatusCellMargin)
//            make.height.equalTo(44) //高度由内容决定.
        }
        
        // retweeteView.
        retweeteView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(contentView)
            make.top.equalTo(originView.snp_bottom)
        }
        
        // 设置bottomView.
        bottomView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(contentView)
            make.height.equalTo(30)
            bottomViewTopCons = make.top.equalTo(retweeteView.snp_bottom).constraint
        }
        
        // 设置contenView的约束,实现自适应约束.
        contentView.snp_makeConstraints { (make) -> Void in
            make.top.leading.trailing.equalTo(self)
            make.bottom.equalTo(bottomView)
        }
    }

    // MARK - 懒加载
    // originView.
    private lazy var originView:TJOriginView = TJOriginView()
    
    // 被转发的View.
    private lazy var retweeteView:TJRetweeteView = TJRetweeteView()
    
    // 底部view
    private lazy var bottomView:TJBottomView = TJBottomView()
    
}
